package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.FeedbackService;

@Service("feedbackService")
public class FeedbackServiceImpl extends AbsService implements FeedbackService {

	private Logger log = LoggerFactory.getLogger(FeedbackServiceImpl.class);
	
	@Override
	public JSONObject addFeedbackInfo(String userName, String phone, String email, String content, String referUrl,
			String sex, Integer userId) {
		
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("FEEDBACK_ADD");
		String[] otherParams = {ApiUrlParamEnum.username.getParamName(), userName,
				ApiUrlParamEnum.phone.getParamName(), phone,
				ApiUrlParamEnum.email.getParamName(), email,
				ApiUrlParamEnum.content.getParamName(), content,
				ApiUrlParamEnum.traceReferUrl.getParamName(), referUrl,
				ApiUrlParamEnum.sex.getParamName(), sex,
				ApiUrlParamEnum.userId.getParamName(), userId.toString()
		};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[addFeedbackInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),  Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
}
